/*
 * Copyright 2011 Katsunori Koyanagi
 *
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
 * use this file except in compliance with the License. You may obtain a copy of
 * the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations under
 * the License.
 */

package sweezy.swf.tools.abcdump
{
	import sweezy.core.utils.StringUtils;
	import sweezy.swf.abc.structure.MetadataInfo;
	import sweezy.swf.abc.structure.MetadataItemInfo;

	internal class MetadataDef extends ABCElementDef
	{

		private var _metadataInfo:MetadataInfo;

		public function MetadataDef(container:ABCcontainerDef, metadataInfo:MetadataInfo)
		{
			super(container);

			_metadataInfo = metadataInfo;
		}

		override public function dump(stream:IDumpStream):void
		{
			var result:String = "[";
			result += _constantPool.stringConstants[_metadataInfo.name];
			if (_metadataInfo.items.length > 0)
			{
				result += "(";
				var delim:String = "";
				for each (var item:MetadataItemInfo in _metadataInfo.items)
				{
					result += delim;
					result += getItem(item);
					delim = ", ";
				}
				result += ")";
			}
			result += "]";

			stream.println(result);
		}

		private function getItem(item:MetadataItemInfo):String
		{
			var result:String = item.key === 0 ? "" : _constantPool.stringConstants[item.key];
			if (result.length > 0)
			{
				result += "=";
			}
			result += StringUtils.escapeESString(_constantPool.stringConstants[item.val]);

			return result;
		}
	}
}
